package com.zhuge_new.Activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.zhuge_new.Adapter.GatheringAdapter;
import com.zhuge_new.Base.BaseActivity;
import com.zhuge_new.Dialog.Gathering_Dialog;
import com.zhuge_new.R;
import com.zhuge_new.bean.CardListBean;
import com.zhuge_new.bean.ExamBean;
import com.zhuge_new.bean.GatheringBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.ActivityStack;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.GlideUtils;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2018/11/13.
 */

public class GatheringAcivity extends BaseActivity implements View.OnClickListener {

    private SwipeMenuRecyclerView recyclerView_list;
    private ImageView deposit_back;
    private TextView deposit_text;
    private GatheringAdapter adapter;
    private CardListBean cardListBean;
    private ExamBean examBean;
    private GatheringBean gatheringBean;
    private String check = "0";
    private TextView gather_title;
    private String title = "全部";
    private ImageView gathering_logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gathering);
        init_title();
        getData();
        getBank();
    }

    @Override
    public void init_title() {
        super.init_title();
        TextView tv_title = findViewById(R.id.tv_nav_title);
        tv_title.setText("收款记录");
        ImageView iv_back = findViewById(R.id.iv_nav_back);
        LinearLayout gather_Filter = findViewById(R.id.gather_Filter);
        recyclerView_list = findViewById(R.id.recycle_list);
        gather_title = findViewById(R.id.gather_title);
        recyclerView_list.setLayoutManager(new LinearLayoutManager(this));
        recyclerView_list.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        deposit_back = findViewById(R.id.deposit_back);
        deposit_text = findViewById(R.id.deposit_text);
        gathering_logo = findViewById(R.id.gathering_logo);
        gather_Filter.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_nav_back:
                ActivityStack.getScreenManager().popOneActivity(GatheringAcivity.class);
                break;
            case R.id.gather_Filter:
                gather_dialog();
                break;
        }
    }

    private void gather_dialog() {
        final Gathering_Dialog dialog = new Gathering_Dialog(baseContext, examBean.getCreditcards(), check);
        dialog.onCreateView();
        dialog.setUiBeforShow();
        dialog.setDialogClickListener(new Gathering_Dialog.DialogClickListener() {
            @Override
            public void onItemClick(String CreditcardId) {
                getScreen(CreditcardId);
                check = CreditcardId;
                getUpdate();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.receiveRecord)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("page", pageNum)
                .params("rows", "15")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj = new JSONObject(response.body()).getJSONObject("object");
                            String receives = obj.getString("receives");
                            if (receives.isEmpty()) {
                                deposit_text.setVisibility(View.VISIBLE);
                                deposit_back.setVisibility(View.VISIBLE);
                            } else {
                                deposit_text.setVisibility(View.GONE);
                                deposit_back.setVisibility(View.GONE);
                                gatheringBean = (GatheringBean) new DataParse().getBean(response.body(), GatheringBean.class, 1);
                                adapter = new GatheringAdapter(baseContext, gatheringBean.getObject().getReceives());
                                if (gatheringBean.getObject().getReceives().size() < 15) {

                                } else {
                                    adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                        @Override
                                        public void onLoadMoreRequested() {
                                            loadMore();
                                        }
                                    }, recyclerView_list);
                                }
                                recyclerView_list.setAdapter(adapter);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void loadMore() {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.receiveRecord)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("page", pageNum)
                .params("rows", "15")
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {

                        try {
                            JSONObject obj = new JSONObject(response.body()).getJSONObject("object");
                            String receives = obj.getString("receives");
                            if (receives.isEmpty()) {
                                pageNum = pageNum - 1;
                                adapter.setEnableLoadMore(false);
                            } else {
                                GatheringBean gatheringBean = (GatheringBean) new DataParse().getBean(response.body(), GatheringBean.class, 1);
                                adapter.addData(gatheringBean.getObject().getReceives());
                                adapter.loadMoreComplete();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    public void getBank() {
        OkGo.<String>post(BaseHttp.creditcard_datas)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        examBean = (ExamBean) new DataParse().getBean(response.body(), ExamBean.class, 0);
                    }
                });
    }

    public void getScreen(String creditcardId) {
        if (creditcardId.equals("0")) {
            creditcardId = "";
        }
        final String finalCreditcardId = creditcardId;
        OkGo.<String>post(BaseHttp.receiveRecord)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("page", pageNum)
                .params("rows", "15")
                .params("creditcardId", creditcardId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        gatheringBean.getObject().getReceives().clear();
                        try {
                            JSONObject obj = new JSONObject(response.body()).getJSONObject("object");
                            String receives = obj.getString("receives");
                            if (receives.isEmpty()) {
                                deposit_text.setVisibility(View.VISIBLE);
                                deposit_back.setVisibility(View.VISIBLE);
                            } else {
                                deposit_text.setVisibility(View.GONE);
                                deposit_back.setVisibility(View.GONE);
                                gatheringBean = (GatheringBean) new DataParse().getBean(response.body(), GatheringBean.class, 1);
                                adapter = new GatheringAdapter(baseContext, gatheringBean.getObject().getReceives());
                                if (gatheringBean.getObject().getReceives().size() < 15) {

                                } else {
                                    adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                                        @Override
                                        public void onLoadMoreRequested() {
                                            loadMore(finalCreditcardId);
                                        }
                                    }, recyclerView_list);
                                }
                                recyclerView_list.setAdapter(adapter);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }

    private void loadMore(String creditcardId) {
        pageNum = pageNum + 1;
        OkGo.<String>post(BaseHttp.receiveRecord)
                .tag(this)
                .headers("token", PreferencesUtils.getString(baseContext, "token"))
                .params("page", pageNum)
                .params("rows", "15")
                .params("creditcardId", creditcardId)
                .execute(new StringDialogCallback(baseContext) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {

                        try {
                            JSONObject obj = new JSONObject(response.body()).getJSONObject("object");
                            String receives = obj.getString("receives");
                            if (receives.isEmpty()) {
                                pageNum = pageNum - 1;
                                adapter.setEnableLoadMore(false);
                            } else {
                                GatheringBean gatheringBean = (GatheringBean) new DataParse().getBean(response.body(), GatheringBean.class, 1);
                                adapter.addData(gatheringBean.getObject().getReceives());
                                adapter.loadMoreComplete();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    public void getUpdate() {

        if (check.equals("0")) {
            gathering_logo.setVisibility(View.GONE);
            gather_title.setText("全部");
        } else {
            for (int i = 0; i < examBean.getCreditcards().size(); i++) {
                if (examBean.getCreditcards().get(i).getCreditcardId().equals(check)) {
                    gathering_logo.setVisibility(View.VISIBLE);
                    GlideUtils.loadClippingImageSize(baseContext,BaseHttp.baseImg1+examBean.getCreditcards().get(i).getLogoUrl(),
                            80,80,gathering_logo);
                    gather_title.setText(examBean.getCreditcards().get(i).getBank()+"尾号("+examBean.getCreditcards().get(i).getCreditcardSuf()+")");
                }

            }

        }
    }
}
